home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / RCS / rand.c,v < prev    next >
Encoding:
Text File  |  1991-10-03  |  14.4 KB  |  526 lines

  1. head     1.5;
  2. branch   ;
  3. access   ;
  4. symbols  sprited:1.5.1;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.5
  10. date     89.03.22.00.47.24;  author rab;  state Exp;
  11. branches 1.5.1.1;
  12. next     1.4;
  13.  
  14. 1.4
  15. date     88.07.29.17.04.29;  author ouster;  state Exp;
  16. branches ;
  17. next     1.3;
  18.  
  19. 1.3
  20. date     88.07.28.13.02.15;  author ouster;  state Exp;
  21. branches ;
  22. next     1.2;
  23.  
  24. 1.2
  25. date     88.07.25.14.19.29;  author ouster;  state Exp;
  26. branches ;
  27. next     1.1;
  28.  
  29. 1.1
  30. date     88.05.21.14.46.00;  author ouster;  state Exp;
  31. branches ;
  32. next     ;
  33.  
  34. 1.5.1.1
  35. date     91.10.02.22.51.51;  author kupfer;  state Exp;
  36. branches ;
  37. next     ;
  38.  
  39.  
  40. desc
  41. @@
  42.  
  43.  
  44. 1.5
  45. log
  46. @*** empty log message ***
  47. @
  48. text
  49. @/* 
  50.  * rand.c --
  51.  *
  52.  *    Contains the source code for the "random", "srandom", "rand",
  53.  *    and "srand" library procedures.  Taken from BSD.
  54.  *
  55.  * Copyright 1983, 1988 Regents of the University of California
  56.  * Permission to use, copy, modify, and distribute this
  57.  * software and its documentation for any purpose and without
  58.  * fee is hereby granted, provided that the above copyright
  59.  * notice appear in all copies.  The University of California
  60.  * makes no representations about the suitability of this
  61.  * software for any purpose.  It is provided "as is" without
  62.  * express or implied warranty.
  63.  */
  64.  
  65. #ifndef lint
  66. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/rand.c,v 1.4 88/07/29 17:04:29 ouster Exp Locker: rab $ SPRITE (Berkeley)";
  67. #endif not lint
  68.  
  69. #include    <stdio.h>
  70. #include        <stdlib.h>
  71.  
  72. /*
  73.  * rand.c:
  74.  * An improved random number generation package.  In addition to the standard
  75.  * rand()/srand() like interface, this package also has a special state info
  76.  * interface.  The initstate() routine is called with a seed, an array of
  77.  * bytes, and a count of how many bytes are being passed in; this array is then
  78.  * initialized to contain information for random number generation with that
  79.  * much state information.  Good sizes for the amount of state information are
  80.  * 32, 64, 128, and 256 bytes.  The state can be switched by calling the
  81.  * setstate() routine with the same array as was initiallized with initstate().
  82.  * By default, the package runs with 128 bytes of state information and
  83.  * generates far better random numbers than a linear congruential generator.
  84.  * If the amount of state information is less than 32 bytes, a simple linear
  85.  * congruential R.N.G. is used.
  86.  * Internally, the state information is treated as an array of longs; the
  87.  * zeroeth element of the array is the type of R.N.G. being used (small
  88.  * integer); the remainder of the array is the state information for the
  89.  * R.N.G.  Thus, 32 bytes of state information will give 7 longs worth of
  90.  * state information, which will allow a degree seven polynomial.  (Note: the 
  91.  * zeroeth word of state information also has some other information stored
  92.  * in it -- see setstate() for details).
  93.  * The random number generation technique is a linear feedback shift register
  94.  * approach, employing trinomials (since there are fewer terms to sum up that
  95.  * way).  In this approach, the least significant bit of all the numbers in
  96.  * the state table will act as a linear feedback shift register, and will have
  97.  * period 2^deg - 1 (where deg is the degree of the polynomial being used,
  98.  * assuming that the polynomial is irreducible and primitive).  The higher
  99.  * order bits will have longer periods, since their values are also influenced
  100.  * by pseudo-random carries out of the lower bits.  The total period of the
  101.  * generator is approximately deg*(2**deg - 1); thus doubling the amount of
  102.  * state information has a vast influence on the period of the generator.
  103.  * Note: the deg*(2**deg - 1) is an approximation only good for large deg,
  104.  * when the period of the shift register is the dominant factor.  With deg
  105.  * equal to seven, the period is actually much longer than the 7*(2**7 - 1)
  106.  * predicted by this formula.
  107.  */
  108.  
  109.  
  110.  
  111. /*
  112.  * For each of the currently supported random number generators, we have a
  113.  * break value on the amount of state information (you need at least this
  114.  * many bytes of state info to support this random number generator), a degree
  115.  * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
  116.  * the separation between the two lower order coefficients of the trinomial.
  117.  */
  118.  
  119. #define        TYPE_0        0        /* linear congruential */
  120. #define        BREAK_0        8
  121. #define        DEG_0        0
  122. #define        SEP_0        0
  123.  
  124. #define        TYPE_1        1        /* x**7 + x**3 + 1 */
  125. #define        BREAK_1        32
  126. #define        DEG_1        7
  127. #define        SEP_1        3
  128.  
  129. #define        TYPE_2        2        /* x**15 + x + 1 */
  130. #define        BREAK_2        64
  131. #define        DEG_2        15
  132. #define        SEP_2        1
  133.  
  134. #define        TYPE_3        3        /* x**31 + x**3 + 1 */
  135. #define        BREAK_3        128
  136. #define        DEG_3        31
  137. #define        SEP_3        3
  138.  
  139. #define        TYPE_4        4        /* x**63 + x + 1 */
  140. #define        BREAK_4        256
  141. #define        DEG_4        63
  142. #define        SEP_4        1
  143.  
  144.  
  145. /*
  146.  * Array versions of the above information to make code run faster -- relies
  147.  * on fact that TYPE_i == i.
  148.  */
  149.  
  150. #define        MAX_TYPES    5        /* max number of types above */
  151.  
  152. static  int        degrees[ MAX_TYPES ]    = { DEG_0, DEG_1, DEG_2,
  153.                                 DEG_3, DEG_4 };
  154.  
  155. static  int        seps[ MAX_TYPES ]    = { SEP_0, SEP_1, SEP_2,
  156.                                 SEP_3, SEP_4 };
  157.  
  158.  
  159.  
  160. /*
  161.  * Initially, everything is set up as if from :
  162.  *        initstate( 1, &randtbl, 128 );
  163.  * Note that this initialization takes advantage of the fact that srand()
  164.  * advances the front and rear pointers 10*rand_deg times, and hence the
  165.  * rear pointer which starts at 0 will also end up at zero; thus the zeroeth
  166.  * element of the state information, which contains info about the current
  167.  * position of the rear pointer is just
  168.  *    MAX_TYPES*(rptr - state) + TYPE_3 == TYPE_3.
  169.  */
  170.  
  171. static  long        randtbl[ DEG_3 + 1 ]    = { TYPE_3,
  172.                 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 
  173.                 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 
  174.                 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 
  175.                 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 
  176.                 0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7, 
  177.                 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 
  178.                 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 
  179.                     0xf5ad9d0e, 0x8999220b, 0x27fb47b9 };
  180.  
  181. /*
  182.  * fptr and rptr are two pointers into the state info, a front and a rear
  183.  * pointer.  These two pointers are always rand_sep places aparts, as they cycle
  184.  * cyclically through the state information.  (Yes, this does mean we could get
  185.  * away with just one pointer, but the code for random() is more efficient this
  186.  * way).  The pointers are left positioned as they would be from the call
  187.  *            initstate( 1, randtbl, 128 )
  188.  * (The position of the rear pointer, rptr, is really 0 (as explained above
  189.  * in the initialization of randtbl) because the state table pointer is set
  190.  * to point to randtbl[1] (as explained below).
  191.  */
  192.  
  193. static  long        *fptr            = &randtbl[ SEP_3 + 1 ];
  194. static  long        *rptr            = &randtbl[ 1 ];
  195.  
  196.  
  197.  
  198. /*
  199.  * The following things are the pointer to the state information table,
  200.  * the type of the current generator, the degree of the current polynomial
  201.  * being used, and the separation between the two pointers.
  202.  * Note that for efficiency of random(), we remember the first location of
  203.  * the state information, not the zeroeth.  Hence it is valid to access
  204.  * state[-1], which is used to store the type of the R.N.G.
  205.  * Also, we remember the last location, since this is more efficient than
  206.  * indexing every time to find the address of the last element to see if
  207.  * the front and rear pointers have wrapped.
  208.  */
  209.  
  210. static  long        *state            = &randtbl[ 1 ];
  211.  
  212. static  int        rand_type        = TYPE_3;
  213. static  int        rand_deg        = DEG_3;
  214. static  int        rand_sep        = SEP_3;
  215.  
  216. static  long        *end_ptr        = &randtbl[ DEG_3 + 1 ];
  217.  
  218.  
  219. /*
  220.  * srandom:
  221.  * Initialize the random number generator based on the given seed.  If the
  222.  * type is the trivial no-state-information type, just remember the seed.
  223.  * Otherwise, initializes state[] based on the given "seed" via a linear
  224.  * congruential generator.  Then, the pointers are set to known locations
  225.  * that are exactly rand_sep places apart.  Lastly, it cycles the state
  226.  * information a given number of times to get rid of any initial dependencies
  227.  * introduced by the L.C.R.N.G.
  228.  * Note that the initialization of randtbl[] for default usage relies on
  229.  * values produced by this routine.
  230.  */
  231.  
  232. srandom( seed )
  233.     int seed;
  234.  
  235. {
  236.     register  int        i;
  237.     unsigned  int        x = seed;
  238.  
  239.     if(  rand_type  ==  TYPE_0  )  {
  240.         state[ 0 ] = x;
  241.     }
  242.     else  {
  243.         state[ 0 ] = x;
  244.         for( i = 1; i < rand_deg; i++ )  {
  245.         state[i] = 1103515245*state[i - 1] + 12345;
  246.         }
  247.         fptr = &state[ rand_sep ];
  248.         rptr = &state[ 0 ];
  249.         for( i = 0; i < 10*rand_deg; i++ )  rand();
  250.     }
  251. }
  252.  
  253.  
  254.  
  255. /*
  256.  * initstate:
  257.  * Initialize the state information in the given array of n bytes for
  258.  * future random number generation.  Based on the number of bytes we
  259.  * are given, and the break values for the different R.N.G.'s, we choose
  260.  * the best (largest) one we can and set things up for it.  srand() is
  261.  * then called to initialize the state information.
  262.  * Note that on return from srand(), we set state[-1] to be the type
  263.  * multiplexed with the current value of the rear pointer; this is so
  264.  * successive calls to initstate() won't lose this information and will
  265.  * be able to restart with setstate().
  266.  * Note: the first thing we do is save the current state, if any, just like
  267.  * setstate() so that it doesn't matter when initstate is called.
  268.  * Returns a pointer to the old state.
  269.  */
  270.  
  271. char  *
  272. initstate( seed, arg_state, n )
  273.  
  274.     unsigned        seed;            /* seed for R. N. G. */
  275.     char        *arg_state;        /* pointer to state array */
  276.     int            n;            /* # bytes of state info */
  277. {
  278.     register  char        *ostate        = (char *)( &state[ -1 ] );
  279.  
  280.     if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
  281.     else  state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type;
  282.     if(  n  <  BREAK_1  )  {
  283.         if(  n  <  BREAK_0  )  {
  284.         fprintf( stderr, "initstate: not enough state (%d bytes) with which to do jack; ignored.\n", n );
  285.         return ostate;
  286.         }
  287.         rand_type = TYPE_0;
  288.         rand_deg = DEG_0;
  289.         rand_sep = SEP_0;
  290.     }
  291.     else  {
  292.         if(  n  <  BREAK_2  )  {
  293.         rand_type = TYPE_1;
  294.         rand_deg = DEG_1;
  295.         rand_sep = SEP_1;
  296.         }
  297.         else  {
  298.         if(  n  <  BREAK_3  )  {
  299.             rand_type = TYPE_2;
  300.             rand_deg = DEG_2;
  301.             rand_sep = SEP_2;
  302.         }
  303.         else  {
  304.             if(  n  <  BREAK_4  )  {
  305.             rand_type = TYPE_3;
  306.             rand_deg = DEG_3;
  307.             rand_sep = SEP_3;
  308.             }
  309.             else  {
  310.             rand_type = TYPE_4;
  311.             rand_deg = DEG_4;
  312.             rand_sep = SEP_4;
  313.             }
  314.         }
  315.         }
  316.     }
  317.     state = &(  ( (long *)arg_state )[1]  );    /* first location */
  318.     end_ptr = &state[ rand_deg ];    /* must set end_ptr before srand */
  319.     srandom( (int) seed );
  320.     if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
  321.     else  state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type;
  322.     return( ostate );
  323. }
  324.  
  325.  
  326.  
  327. /*
  328.  * setstate:
  329.  * Restore the state from the given state array.
  330.  * Note: it is important that we also remember the locations of the pointers
  331.  * in the current state information, and restore the locations of the pointers
  332.  * from the old state information.  This is done by multiplexing the pointer
  333.  * location into the zeroeth word of the state information.
  334.  * Note that due to the order in which things are done, it is OK to call
  335.  * setstate() with the same state as the current state.
  336.  * Returns a pointer to the old state information.
  337.  */
  338.  
  339. char  *
  340. setstate( arg_state )
  341.  
  342.     char        *arg_state;
  343. {
  344.     register  long        *new_state    = (long *)arg_state;
  345.     register  int        type        = new_state[0]%MAX_TYPES;
  346.     register  int        rear        = new_state[0]/MAX_TYPES;
  347.     char            *ostate        = (char *)( &state[ -1 ] );
  348.  
  349.     if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
  350.     else  state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type;
  351.     switch(  type  )  {
  352.         case  TYPE_0:
  353.         case  TYPE_1:
  354.         case  TYPE_2:
  355.         case  TYPE_3:
  356.         case  TYPE_4:
  357.         rand_type = type;
  358.         rand_deg = degrees[ type ];
  359.         rand_sep = seps[ type ];
  360.         break;
  361.  
  362.         default:
  363.         fprintf( stderr, "setstate: state info has been munged; not changed.\n" );
  364.     }
  365.     state = &new_state[ 1 ];
  366.     if(  rand_type  !=  TYPE_0  )  {
  367.         rptr = &state[ rear ];
  368.         fptr = &state[ (rear + rand_sep)%rand_deg ];
  369.     }
  370.     end_ptr = &state[ rand_deg ];        /* set end_ptr too */
  371.     return( ostate );
  372. }
  373.  
  374.  
  375.  
  376. /*
  377.  * random:
  378.  * If we are using the trivial TYPE_0 R.N.G., just do the old linear
  379.  * congruential bit.  Otherwise, we do our fancy trinomial stuff, which is the
  380.  * same in all ther other cases due to all the global variables that have been
  381.  * set up.  The basic operation is to add the number at the rear pointer into
  382.  * the one at the front pointer.  Then both pointers are advanced to the next
  383.  * location cyclically in the table.  The value returned is the sum generated,
  384.  * reduced to 31 bits by throwing away the "least random" low bit.
  385.  * Note: the code takes advantage of the fact that both the front and
  386.  * rear pointers can't wrap on the same call by not testing the rear
  387.  * pointer if the front one has wrapped.
  388.  * Returns a 31-bit random number.
  389.  */
  390.  
  391. long
  392. random()
  393. {
  394.     long        i;
  395.  
  396.     if(  rand_type  ==  TYPE_0  )  {
  397.         i = state[0] = ( state[0]*1103515245 + 12345 )&0x7fffffff;
  398.     }
  399.     else  {
  400.         *fptr += *rptr;
  401.         i = (*fptr >> 1)&0x7fffffff;    /* chucking least random bit */
  402.         if(  ++fptr  >=  end_ptr  )  {
  403.         fptr = state;
  404.         ++rptr;
  405.         }
  406.         else  {
  407.         if(  ++rptr  >=  end_ptr  )  rptr = state;
  408.         }
  409.     }
  410.     return( i );
  411. }
  412.  
  413. /*
  414.  * For compatibility with the old UNIX rand and srand:
  415.  */
  416.  
  417. rand()
  418. {
  419.     return random();
  420. }
  421.  
  422. srand(seed)
  423.     int seed;
  424. {
  425.     srandom(seed);
  426. }
  427. @
  428.  
  429.  
  430. 1.5.1.1
  431. log
  432. @Initial branch for Sprite server.
  433. @
  434. text
  435. @d18 1
  436. a18 1
  437. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/rand.c,v 1.5 89/03/22 00:47:24 rab Exp $ SPRITE (Berkeley)";
  438. @
  439.  
  440.  
  441. 1.4
  442. log
  443. @Lint.
  444. @
  445. text
  446. @d18 1
  447. a18 1
  448. static char rcsid[] = "$Header: rand.c,v 1.3 88/07/28 13:02:15 ouster Exp $ SPRITE (Berkeley)";
  449. d22 1
  450. @
  451.  
  452.  
  453. 1.3
  454. log
  455. @Added back random and srandom.
  456. @
  457. text
  458. @d18 1
  459. a18 1
  460. static char rcsid[] = "$Header: rand.c,v 1.2 88/07/25 14:19:29 ouster Exp $ SPRITE (Berkeley)";
  461. d270 1
  462. a270 1
  463.     srand( seed );
  464. @
  465.  
  466.  
  467. 1.2
  468. log
  469. @Lint.
  470. @
  471. text
  472. @d4 2
  473. a5 2
  474.  *    Contains the source code for the "rand" and "srand" library
  475.  *    procedures.  Taken from BSD
  476. d18 1
  477. a18 1
  478. static char rcsid[] = "$Header: rand.c,v 1.1 88/05/21 14:46:00 ouster Exp $ SPRITE (Berkeley)";
  479. d136 1
  480. a136 1
  481.  * away with just one pointer, but the code for rand() is more efficient this
  482. d153 1
  483. a153 1
  484.  * Note that for efficiency of rand(), we remember the first location of
  485. d171 1
  486. a171 1
  487.  * srand:
  488. d183 2
  489. a184 2
  490. void
  491. srand( x )
  492. a185 1
  493.     unsigned int        x;
  494. d188 1
  495. d328 1
  496. a328 1
  497.  * rand:
  498. d342 2
  499. a343 2
  500. int
  501. rand()
  502. d346 1
  503. a346 1
  504.     
  505. d362 15
  506. @
  507.  
  508.  
  509. 1.1
  510. log
  511. @Initial revision
  512. @
  513. text
  514. @d18 1
  515. a18 1
  516. static char rcsid[] = "$Header: proto.c,v 1.2 88/03/11 08:39:08 ouster Exp $ SPRITE (Berkeley)";
  517. d188 1
  518. a188 1
  519.         register  int        i, j;
  520. a193 1
  521.         j = 1;
  522. d236 1
  523. a236 1
  524.         return;
  525. @
  526.